package cn.net.chenbao.medical.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 获取SharedPreference数据的工具类
 *
 * @author Bubble
 */
public class SharedPreferenceUtils {
    private static final String USER_INFO = "shared_save_info";
    public static final String CYK = "cyk";

    private static SharedPreferenceUtils utils;
    private static SharedPreferences mSharedPreferences;
    private static SharedPreferences.Editor editor;

    /**
     * 登录的sessionId
     */
    private static String SHARED_KEY_LOGIN_ID = "login_id";
    /**
     * operName
     */
    private static String SHARED_KEY_OPER_NAME = "oper_name";
    /**
     * hostAddress
     */
    private static String SHARED_KEY_HOST_ADDRESS = "host_address";
    /**
     * 登录的userId
     */
    private static String SHARED_KEY_USER_ID = "userId";
    /**
     * 保存电话号码
     */
    private static String SHARED_KEY_PHONE_NUM = "phone_num";
    /**
     * APP版本
     */
    private static String SHARED_KEY_APPVERSION = "app_version";
    /**
     * 用户密码
     */
    private static String SHARED_KEY_USER_PSW = "user_psw";

    private SharedPreferenceUtils(Context cxt) {
        mSharedPreferences = cxt.getSharedPreferences(USER_INFO,
                Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
    }

    public static synchronized void init(Context cxt) {
        if (utils == null) {
            utils = new SharedPreferenceUtils(cxt);
        }
    }

    public synchronized static SharedPreferenceUtils getInstance() {
        if (utils == null) {
            throw new RuntimeException("please init first!");
        }
        return utils;
    }

    /**
     * 保存会话id
     *
     * @param sessionId
     */
    public void saveSessionId(String sessionId) {
        editor.putString(SHARED_KEY_LOGIN_ID, sessionId);
        editor.commit();
    }

    /**
     * 获取会话id
     *
     * @return
     */
    public String getSessionId() {
        return mSharedPreferences.getString(SHARED_KEY_LOGIN_ID, "");
    }


    /**
     * operName
     *
     * @param operName
     */
    public void saveOperName(String operName) {
        editor.putString(SHARED_KEY_OPER_NAME, operName);
        editor.commit();
    }

    /**
     * 获取会话id
     *
     * @return
     */
    public String getOperName() {
        return mSharedPreferences.getString(SHARED_KEY_OPER_NAME, "");
    }

/**
     * operName
     *
     * @param hostAddress
     */
    public void saveHostAddresse(String hostAddress) {
        editor.putString(SHARED_KEY_HOST_ADDRESS, hostAddress);
        editor.commit();
    }

    /**
     * 获取会话id
     *
     * @return
     */
    public String getHostAddress() {
        return mSharedPreferences.getString(SHARED_KEY_HOST_ADDRESS, "");
    }





    /**
     * 保存电话
     *
     * @param value
     */
    public void savePhoneNum(String value) {
        editor.putString(SHARED_KEY_PHONE_NUM, value);
        editor.commit();
    }

    /**
     * 获得电话
     *
     * @return
     */
    public String getPhoneNum() {
        return mSharedPreferences.getString(SHARED_KEY_PHONE_NUM, "");
    }





    /**
     * 个人的清除登录数据 ,需要记住密码和用户名
     */
    public void clearLoginData() {
        editor.putString(SHARED_KEY_LOGIN_ID, null);
        editor.commit();
    }

    /**
     * 保存版本号
     *
     * @param value
     */
    public void saveAppVersion(int value) {
        editor.putInt(SHARED_KEY_APPVERSION, value);
        editor.commit();
    }

    /**
     * 获得版本号
     *
     * @return
     */
    public int getAppVersion() {
        return mSharedPreferences.getInt(SHARED_KEY_APPVERSION, 0);
    }



}
